/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package viewControl;

import entity.Laerer;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.Scanner;
import javax.swing.JOptionPane;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

/**
 *
 * @author Oli og Anette
 */
public class Login extends javax.swing.JFrame {

    private Session session;

    /**
     * Creates new form Login
     */
    public Login() {
        String host = "";
        String port = "";
        String dbName = "";
        String uName = "";
        String pass = "";
        try {
            FileInputStream fis = new FileInputStream("dbconfig.cfg");
            Scanner s = new Scanner(fis);
            while (s.hasNextLine()) {
                host = s.nextLine();
                port = s.nextLine();
                dbName = s.nextLine();
                uName = s.nextLine();
                pass = s.nextLine();
            }
            s.close();
            fis.close();
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null, "Kunne ikke læse dbconfig.cfg filen:\n" + ex.getLocalizedMessage(), "Fejl!", 0);
            System.exit(0);
        }
        AnnotationConfiguration conf = new AnnotationConfiguration().configure();
        conf.setProperty("hibernate.connection.url", "jdbc:mysql://" + host + ":" + port + "/" + dbName);
        conf.setProperty("hibernate.connection.username", uName);
        conf.setProperty("hibernate.connection.password", pass);
        SessionFactory sf = conf.buildSessionFactory();

        session = sf.openSession();
        initComponents();
        setLocationRelativeTo(null);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        cbUserType = new javax.swing.JComboBox();
        btnLogin = new javax.swing.JButton();
        cbTeacherName = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Login v 1.0 Beta");

        cbUserType.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Bruger Type", "Lærer", "Pædagog", "Sekretær", "Admin" }));
        cbUserType.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbUserTypeActionPerformed(evt);
            }
        });

        btnLogin.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        btnLogin.setText("Login");
        btnLogin.setEnabled(false);
        btnLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoginActionPerformed(evt);
            }
        });

        cbTeacherName.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Lærer Navn" }));
        cbTeacherName.setEnabled(false);
        cbTeacherName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbTeacherNameActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setText("Vælg Bruger Type");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setText("Vælg Lærer");
        jLabel2.setEnabled(false);

        jLabel3.setText("  ");
        jLabel3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel3MouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(65, 65, 65)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cbUserType, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(btnLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(cbTeacherName, javax.swing.GroupLayout.PREFERRED_SIZE, 160, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(81, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(47, 47, 47)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cbUserType, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(cbTeacherName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(btnLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 35, Short.MAX_VALUE)
                .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cbUserTypeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbUserTypeActionPerformed

        if (cbUserType.getSelectedIndex() == 1) {
            jLabel2.setEnabled(true);
            cbTeacherName.removeAllItems();
            cbTeacherName.addItem("Vælg lærer");
            String hql = "from Laerer";
            try {
                session.beginTransaction();
                Query q = session.createQuery(hql);
                List resultList = q.list();
                for (int i = 0; i < resultList.size(); i++) {
                    cbTeacherName.addItem((Laerer) resultList.get(i));
                }
                session.getTransaction().commit();
            } catch (HibernateException he) {
                System.out.println(he.getLocalizedMessage());
            }

            cbTeacherName.setEnabled(true);

        } else {
            jLabel2.setEnabled(false);
            cbTeacherName.setEnabled(false);
            cbTeacherName.setSelectedIndex(0);
            if (cbUserType.getSelectedIndex() > 1) {
                btnLogin.setEnabled(true);
            } else {
                btnLogin.setEnabled(false);
            }
        }
    }//GEN-LAST:event_cbUserTypeActionPerformed

    private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoginActionPerformed

        if (cbUserType.getSelectedIndex() == 1) {
            TeacherGUI tg = new TeacherGUI(session, (Laerer) cbTeacherName.getSelectedItem());
            tg.setVisible(true);
        }
        if (cbUserType.getSelectedIndex() == 2) {
            TherapistGUI thg = new TherapistGUI(session);
            thg.setVisible(true);
        }
        if (cbUserType.getSelectedIndex() == 3) {
            SecretaryGUI sg = new SecretaryGUI(session);
            sg.setVisible(true);
        }
        if (cbUserType.getSelectedIndex() == 4) {
            int result = JOptionPane.showConfirmDialog(null, "\nEr du sikker på du vil logge ind som Administrator?\n(Dette login giver dig adgang til alle funktioner)\n", "Advarsel", 2, 2);
            if (result == JOptionPane.OK_OPTION) {
                AdminGUI ag = new AdminGUI(session);
                ag.setVisible(true);
            }
        }
    }//GEN-LAST:event_btnLoginActionPerformed

    private void cbTeacherNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbTeacherNameActionPerformed
        if (cbTeacherName.getSelectedIndex() != 0) {
            btnLogin.setEnabled(true);
        } else {
            btnLogin.setEnabled(false);
        }
    }//GEN-LAST:event_cbTeacherNameActionPerformed

    private void jLabel3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel3MouseClicked
        JOptionPane.showMessageDialog(null, "Dette program blev lavet som et 2 semester eksamens projekt af: \n------------------\nJeppe Andersen\nMartin Enderleit\nMorten Bormeth\nOliver Krøjer\n------------------", "The makers :D", 1);
    }//GEN-LAST:event_jLabel3MouseClicked

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
//            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
//                if (javax.swing.UIManager.getSystemLookAndFeelClassName().equals(info.getName())) {
//                    javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
//                    System.out.println(javax.swing.UIManager.getSystemLookAndFeelClassName());
//                }
//                System.out.println(javax.swing.UIManager.getSystemLookAndFeelClassName());
//                
//            }
            javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Login().setVisible(true);

            }
        });

    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnLogin;
    private javax.swing.JComboBox cbTeacherName;
    private javax.swing.JComboBox cbUserType;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    // End of variables declaration//GEN-END:variables
}
